import axios from 'axios';
// 服务器地址
// const BASE_URL = 'http://192.168.71.63:8090';
const BASE_URL = 'http://192.168.1.49:8090';
// 获取一级标签
export async function fetchLevel1Tags() {
    try {
        const response = await axios.get(`${BASE_URL}/tag/getTagInfo/1`);
        return response.data;
    } catch (error) {
        console.error('获取一级分类失败:', error);
        return [];
    }
}

// 获取二三级标签
export async function fetchLevel2Tags(tagId) {
    try {
        const response = await axios.get(`${BASE_URL}/tag/getTagInfoByTagId/${tagId}`);
        return response.data;
    } catch (error) {
        console.error(`获取二三级分类失败:`, error);
        return [];
    }
}

export async function getImageTablesLoop(tagId){
    try {
        const response = await axios.get(`${BASE_URL}/image/getImageTablesLoop/${tagId}`);
        return response.data;
    } catch (error) {
        console.error('循环获取标签及子标签涉及的所有图片:', error);
        return [];
    }
}

export async function addTagInfo(tagData){
    await axios.post(`${BASE_URL}/tag/addTagInfo`, tagData, {
        headers: { 'Content-Type': 'application/json' }
    });
    alert('标签新增成功！');
}


export async function deleteTagInfo(tagId){
    await axios.get(`${BASE_URL}/tag/deleteTagInfo/${tagId}`);
    alert('标签删除成功！');
}

export async function addImageTag(imageTagBeans){
    await axios.post(`${BASE_URL}/tag/addImageTag`, imageTagBeans, {
        headers: { 'Content-Type': 'application/json' }
    });
    alert('图片新增标签成功！');
}

export async function deleteImageTag(imageTagBeans){
    await axios.post(`${BASE_URL}/tag/deleteImageTag`, imageTagBeans, {
        headers: { 'Content-Type': 'application/json' }
    });
    alert('图片删除标签成功！');
}

/**
 * 根据图片 ID 生成图片 URL
 * @param {string} imageId - 图片 ID
 * @returns {string} - 完整的图片 URL
 */
export function getImageUrl(imageId) {
    return `${BASE_URL}/image/getImage?image_id=${imageId}`;
}

export async function getAllFileMonth(){
    const response = await axios.get(`${BASE_URL}/image/getAllFileMonth`);
    if (Array.isArray(response.data)) {
        return response.data;
    } else {
        console.error('接口返回的月份数据格式不正确');
    }
}

export async function getImageTableByFileMonth(pageNum,pageSize,month){
    return await axios.get(`${BASE_URL}/image/getImageTableByFileMonth/${month}`, {
        params: {pageNum, pageSize}
    });
}

export async function getTagLine(imageId){
    const response = await axios.get(`${BASE_URL}/tag/getTagLine/${imageId}`,{
        params:{ imageId}
    });
    return response.data;
}